package containers;

import java.util.Random;
import java.util.concurrent.CopyOnWriteArrayList;

import util.CardValues;

import casino.Card;
import casino.Casino;
import casino.GameCard;
import casino.Player;

/*
 * Make this an arraylist of cards;
 */
public class Deck extends CardContainer{
	
	public Deck(Casino n){
		super(n);
	}
	
	public Deck(Casino c, Card[] t){
		super(c);
		GameCard temp;
		for(int i = 0; i < t.length; i++){
			temp = new GameCard(casino, this, t[i]);
			temp.setPosition(x, y);
			cards.add(temp);
		}
	}
	
	public Deck(Casino n, Player p, int x, int y){
		super(n,p,x,y,CardValues.width,CardValues.height);
	}
	
	public Deck(Casino c, Player p, Card[] t, int x, int y){
		super(c,p,x,y,CardValues.width,CardValues.height);
		GameCard temp;
		for(int i = 0; i < t.length; i++){
			temp = new GameCard(casino, this, t[i]);
			temp.setPosition(x, y);
			cards.add(temp);
		}
	}
	
	public Deck(Casino c, Player p, CopyOnWriteArrayList<GameCard> t, int x, int y){
		super(c,p,x,y,CardValues.width,CardValues.height);
		for(int i = 0; i < t.size(); i++){
			add(t.get(i));
			t.get(i).setPosition(x, y);
			if(!casino.cards.contains(t.get(i)))System.out.println("Lolz");
		}
	}
	
	public void shuffle(){
		Random r = new Random();
		int curRand;
		for(int i = 0; i < cards.size(); i++){
			curRand = r.nextInt(cards.size()-1);
			cards.set(curRand, cards.set(i, cards.get(curRand)));
		}
	}
	
	public void add(GameCard c){
		if(c != null){
			cards.add(0, c);
			c.setParent(this);
			lengthChange();
		}
	}
	
	public void lengthChange(){
		for(int i = cards.size()-1; i >= 0; i--){
			casino.bringToFront(cards.get(i));
			cards.get(i).visible = false;
			cards.get(i).accesible = false;
		}
		for(int i = 0; i < cards.size() && i < 3; i++){
			cards.get(i).visible = true;
			cards.get(i).setPosition(x-3*i, y+3*i);
		}
		if(cards.size()>0)cards.get(0).accesible = true;
	}
	
	public GameCard draw(){
		if(cards.size() != 0){
			GameCard c = cards.get(0);
			remove(c);
			return c;
		}else{
			return null;
		}
	}
	
	public GameCard cardAt(int index){
		return cards.get(index);
	}
	
	public CopyOnWriteArrayList<GameCard> getDeck(){
		return cards;
	}
	
	public void setDeck(CopyOnWriteArrayList<GameCard> c){
		cards = new CopyOnWriteArrayList<GameCard>(c);
	}
	
	public CopyOnWriteArrayList<GameCard> deal(int amount){
		CopyOnWriteArrayList<GameCard> temp = new CopyOnWriteArrayList<GameCard>();
		for(int i = 0; i < amount; i++){
			temp.add(draw());
		}
		return temp;
	}
}
